package com.training1.core.web.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.training1.core.common.dao.Page;
import com.training1.core.entity.IncomeEntity;
import com.training1.core.entity.SystemUserEntity;
import com.training1.core.entity.sumCashEntity;
import com.training1.core.modules.service.IncomeService;

@Controller
@RequestMapping("income")
public class IncomeConntroller {
	//时间工具方法
	public static Date formatDate(String str) throws Exception{
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		 Date date=formatter.parse(str);
		 return date;
	}
	@Resource
	private  IncomeService incomeService;
	
	/**
	 * 进入财务纳表页面
	 * @return
	 */
	@RequestMapping(value={"/",""})
	public String go(){
		return "income/income";
	}
	
	/**
	 * 查询及列表往页面传值
	 * @param page
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("list")
	@ResponseBody
	public Page<IncomeEntity> list(String name,String membername,String cellphone, String startTime,String endTime,String incomeType,String paymentType,
			String paymentChannel,Page<IncomeEntity> page) throws Exception{
		page.getSearch().put("userName", name);
		page.getSearch().put("memberName", membername);
		page.getSearch().put("cellphone", cellphone);
		if(startTime!=null&startTime!=""){//判断接受过来的时间是否为空
			page.getSearch().put("startTime", formatDate(startTime));
		}
		if(endTime!=null&endTime!=""){
			page.getSearch().put("endTime", formatDate(endTime));
		}
		page.getSearch().put("incomeType", incomeType);
		page.getSearch().put("paymentType", paymentType);
		page.getSearch().put("paymentChannel", paymentChannel);
		incomeService.findList(page.getSearch());
		
		//此参数模板用来统计总金额,因为page大小只有200
		Map<String,Object>param=new HashMap<>();
		param.put("userName", name);
		param.put("memberName", membername);
		param.put("cellphone", cellphone);
		if(startTime!=null&startTime!=""){//判断接受过来的时间是否为空
			param.put("startTime", formatDate(startTime));
		}
		if(endTime!=null&endTime!=""){
			param.put("endTime", formatDate(endTime));
		}
		param.put("incomeType", incomeType);
		param.put("paymentType", paymentType);
		param.put("paymentChannel", paymentChannel);
		
		
		//设置页脚的合计显示金额
		IncomeEntity ie=new IncomeEntity();
		List<IncomeEntity> in=incomeService.countCash(param);
		if(in.get(0)!=null){	
			ie.setCash(incomeService.countCash(param).get(0).getCash());
		}
		ie.setCellphone("合计");
		List<IncomeEntity>list=new ArrayList<>();
		list.add(ie);
		page.setFooter(list);
		return page;
	}
	
	//查询全部的订单中心名字
	@RequestMapping(value="/username",method={RequestMethod.GET})
	@ResponseBody
	public List<SystemUserEntity> typelist() {
		List<SystemUserEntity> list = new ArrayList<>();
		SystemUserEntity s=new SystemUserEntity();
		//下拉框的开头拼一个全部
		s.setUserAccount(null);
		s.setuserName("全部");
		list.add(s);
		list.addAll(incomeService.findUserName());
		return list;
	}
	
	//导出表格
	@RequestMapping("export")
	@ResponseBody
	public void export(HttpServletResponse resp,String name,String membername,String cellphone, String startTime,String endTime,String incomeType,String paymentType,
			String paymentChannel) throws Exception{
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userName", name);
		param.put("memberName", membername);
		param.put("cellphone", cellphone);
		if(startTime!=null&startTime!=""){
			param.put("startTime", formatDate(startTime));
		}
		if(endTime!=null&endTime!=""){
			param.put("endTime", formatDate(endTime));
		}
		param.put("incomeType", incomeType);
		param.put("paymentType", paymentType);
		param.put("paymentChannel", paymentChannel);
		incomeService.export(resp, param);
	}
	
	// 查询各订单中心的总金额用柱状图显示
	@RequestMapping("sumCash")
	public void sumCash(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<sumCashEntity>list=incomeService.sumCash();
		/*金额*/  
    	Double[] cash=new Double[list.size()];
    	for(int i=0;i<cash.length;i++){
    		cash[i]=list.get(i).getCash();
    	}
    	/*横轴, 开单中心*/  
        String[]userName=new String[list.size()];
    	for(int i=0;i<userName.length;i++){
    		userName[i]=list.get(i).getUserName();
    	}
    	
    	Map<String, Object> map = new HashMap<>();  
        map.put("cash", cash);    
        map.put("userName", userName);
        response.getWriter().println(JSON.toJSONString(map));  
	}
}
